<?php
/**
 * User: hjd-yao
 * Date: 2019/5/5
 * Time: 14:36
 */

namespace App\Http\Controllers\User\Traits;


use App\Exports\DataExport;
use App\Exports\MultipleDataExport;
use App\Helpers\SelfEncrypt;
use DC\UserCenter\Models\UserBaseModel;
use Illuminate\Http\Request;

trait AjaxUser
{
    /**
     * @title 验证会员信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserInfo(Request $request)
    {
        $user_number = $request->get('user');
        $info = UserBaseModel::where('user_number',$user_number)
            ->select(['member_id','user_number','user_phone','real_name'])->first();
        if(!$info){
            return response()->json(['status'=>0,'msg'=>'会员不存在']);
        }
        return response()->json(['status'=>1,'data'=>$info->toArray()]);
    }

    /**
     * @title 导出会员
     * @param Request $request
     * @return \Illuminate\Http\Response|BinaryFileResponse
     */
    public function exportUserData(Request $request)
    {
        $user_id = $request->input('user_id');
        $userModel = UserBaseModel::select(['user_number','real_name','user_phone']);
        if($user_id){
            $userModel = $userModel->whereIn('member_id',$user_id);
        }

        return (new MultipleDataExport())->setHeadings('会员资料详情',['user_id','会员姓名2','联系方式3'])
            ->setData($userModel,function ($data){
                return $data;
            })->download('user'.SelfEncrypt::createCode().'.xlsx');
    }
}